package com.edushare.common.dao;

import java.util.List;

import com.edushare.common.orm.ORMInterface;
import com.edushare.common.vo.AcademicYearModel;
import com.edushare.common.vo.Universities;

public class AcademicYearModelDAO extends CayenneBasedDAO {

	public AcademicYearModelDAO(ORMInterface ormInterface, Universities univ) {
		super(ormInterface);
		this.univ = univ;
	}

	public void create(int sequenceNo, String semName, int isCurrent) {
		if(getSemester(sequenceNo) == null){
			AcademicYearModel aym = getDataContext().newObject(AcademicYearModel.class);
			aym.setSequenceNo(sequenceNo);
			aym.setSemesterName(semName);
			aym.setIsCurrent(isCurrent);
			aym.setUniversity(univ);
		}
		getDataContext().commitChanges();
	}

	public int getNumberOfSemesters(){
		List<AcademicYearModel> ayList = univ.getAcademicYearModels();
		return ayList.size();
	}
	
	public AcademicYearModel getSemester(int semNumber){
		List<AcademicYearModel> ayList = univ.getAcademicYearModels();
		for(AcademicYearModel ay:ayList){
			if(ay.getSequenceNo() == semNumber)
				return ay; 
		}
		return null;
	}
	
	public String getSemesterName(int semNumber, int year){
		List<AcademicYearModel> ayList = univ.getAcademicYearModels();
		for(AcademicYearModel ay:ayList){
			if(ay.getSequenceNo() == semNumber)
				return ay.getSemesterName() +" "+ year + "-" + (year+1);
		}
		return "";
	}
	
	public void setSemesterName(int semNumber, String semName){
		List<AcademicYearModel> ayList = univ.getAcademicYearModels();
		for(AcademicYearModel ay:ayList){
			if(ay.getSequenceNo() == semNumber)
				ay.setSemesterName(semName);
		}
		getDataContext().commitChanges();
	}
	
	public void setActiveSemester(int semNumber){
		List<AcademicYearModel> ayList = univ.getAcademicYearModels();
		if(semNumber < getNumberOfSemesters()){
			for(AcademicYearModel ay:ayList){
				ay.setIsCurrent(AcademicYearModel.INACTIVE_SEM);
				if(ay.getSequenceNo() == semNumber)
					ay.setIsCurrent(AcademicYearModel.ACTIVE_SEM);
			}
		}
		getDataContext().commitChanges();
	}
	
	public void incrementSemester(){
		if(getActiveSemester().getSequenceNo() == getNumberOfSemesters()-1){
			setActiveSemester(0);
			univ.setCurrentAcadYear(univ.getCurrentAcadYear()+1);
		}else{
			setActiveSemester(getActiveSemester().getSequenceNo()+1);
		}
		getDataContext().commitChanges();
	}
	
	public AcademicYearModel getActiveSemester(){
		List<AcademicYearModel> ayList = univ.getAcademicYearModels();
		for(AcademicYearModel ay:ayList){
			if(ay.getIsCurrent() == AcademicYearModel.ACTIVE_SEM)
				return ay;
		}
		return null;
	}
	
	public String getNextSemAsString(){
		int activeSem = 0;
		int nextYr = 2010;
		
		if(getActiveSemester().getSequenceNo() == getNumberOfSemesters()-1){
			activeSem = 0;
			nextYr = univ.getCurrentAcadYear()+1;
		}else{
			activeSem = getActiveSemester().getSequenceNo()+1;
			nextYr = univ.getCurrentAcadYear();
		}
		
		return getSemesterName(activeSem, nextYr);
	}
	
	private Universities univ;
}
